package com.example.music.Home;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.icu.util.MeasureUnit;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.LinearInterpolator;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextSwitcher;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.music.R;
import com.example.music.base.BaseFragment;
import com.example.music.service.MusicService;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.OnClick;

public class HomeFragment extends BaseFragment implements Runnable, ServiceConnection, SeekBar.OnSeekBarChangeListener {
    @BindView(R.id.btn_prev)
    ImageView playingPre;
    @BindView(R.id.btn_play)
    ImageView playingPlay;
    @BindView(R.id.btn_next)
    ImageView playingNext;
    @BindView(R.id.needle)
    ImageView needle;
    @BindView(R.id.disc)
    ImageView disc;
    @BindView(R.id.sb)
    SeekBar mSeekBar;
    @BindView(R.id.text_switcher)
    TextSwitcher mSwitcher;
    private ObjectAnimator discAnimation,needleAnimation;//自定义指针和唱盘
    private boolean isPlaying = true;//0,1 判断是否处于播放状态
    //声明服务
    private static final String TAG = HomeFragment.class.getSimpleName();
    private MusicService.MusicController mMusicController;
    private boolean running;


    @OnClick({R.id.btn_prev,R.id.btn_play,R.id.btn_next})
    void onClick(View v){
        switch (v.getId()){
            case R.id.btn_prev: //前一曲A
                if (discAnimation != null) {
                    discAnimation.end();
                    playing();
                }
                break;
            case R.id.btn_play://播放中
                if (isPlaying){
                    playing();
                }else {
                    if (needleAnimation != null) {
                        needleAnimation.reverse();
                        needleAnimation.end();
                        mMusicController.pause();
                    }
                    if (discAnimation != null && discAnimation.isRunning()) {
                        discAnimation.cancel();
                        mMusicController.pause();
                        float valueAvatar = (float) discAnimation.getAnimatedValue();
                        discAnimation.setFloatValues(valueAvatar, 360f + valueAvatar);
                    }
                    playingPlay.setImageResource(R.mipmap.btn_pause);
                    isPlaying = true;
                }
                break;
            default:
                break;
        }
    }
    //播放：1、播放音乐 2、动画旋转 3、暂停图片切换为播放按钮图片
    private void playing(){
        needleAnimation.start();
        discAnimation.start();
        playingPlay.setImageResource(R.mipmap.btn_play);
        mMusicController.play();//播放
        isPlaying = false;
    }



    @Override
    protected View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_home,container,false);

    }
    @Override
    protected void initData(@Nullable Bundle savedInstanceState) {
        //指针和唱片部分
        initViews();//定义背景图
        setAnimations();//动画设置

    }
    private void setAnimations() {
        discAnimation = ObjectAnimator.ofFloat(disc, "rotation", 0, 360);
        discAnimation.setDuration(20000);
        discAnimation.setInterpolator(new LinearInterpolator());
        discAnimation.setRepeatCount(ValueAnimator.INFINITE);

        needleAnimation = ObjectAnimator.ofFloat(needle, "rotation", 0, 25);
        needle.setPivotX(0);
        needle.setPivotY(0);
        needleAnimation.setDuration(800);
        needleAnimation.setInterpolator(new LinearInterpolator());

    }

    private void initViews() {
        //滑动条部分
        mSwitcher.setInAnimation(this.getContext(), android.R.anim.fade_in);
        mSwitcher.setOutAnimation(this.getContext(), android.R.anim.fade_out);
        Intent intent = new Intent(this.getContext(), MusicService.class);
        //增加StartService，来增加后台播放功能
        mContext.startService(intent);
        // 绑定服务，使用context来绑定
        // 那个界面需要绑定 就用哪个 Activity
        // 参数1：Intent代表需要绑定哪一个Service
        // 参数2：ServiceConnection回调接口，可以接收到Service连接成功和断开的回调，成功就可以取到对象。
        // 绑定服务 参数2就是服务和指定的对象绑定在一起
        mContext.bindService(intent,this,Context.BIND_AUTO_CREATE);
        mSeekBar.setOnSeekBarChangeListener(this);
    }


    @Override
    public void onStart() {
        super.onStart();
        Thread thread = new Thread(this);
        thread.start();
    }

    @Override
    public void onStop() {
        running = false;
        super.onStop();
    }

    @Override
    public void onDestroy() {
        // 解除绑定
        mContext.unbindService(this);
        super.onDestroy();
    }



    public static HomeFragment newInstance(){
        HomeFragment fragment=new HomeFragment();
        return fragment;
    }


    @Override
    public void onServiceConnected(ComponentName componentName, IBinder service) {
        mMusicController = ((MusicService.MusicController) service);

    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        mMusicController = null;

    }

    @Override
    public void run() {

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        mMusicController.setPosition(seekBar.getProgress());

    }
}